<template>
	<view style="padding: 40rpx;">
		<uv-button text="创建卡密" type="success" @click="createCode"></uv-button>

		<view style="margin-top: 40rpx;">
			<uni-easyinput v-model="value"  @input="e=>{value = e}" placeholder="请输入卡密"></uni-easyinput>
		</view>
		
		<view style="margin-top: 40rpx;">
			<uv-button text="消除卡密"  @click="removeCode" type="error"></uv-button>
		</view>
		
		<view style="margin-top: 40rpx;">
			<uni-easyinput v-model="reset"  @input="e=>{reset = e}" placeholder="请输入卡密"></uni-easyinput>
		</view>
		
		<view style="margin-top: 40rpx;">
			<uv-button text="重置卡密"  @click="resetCode" type="warning"></uv-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				value:'',
				reset:''
			}
		},
		onLoad() {

		},
		methods: {
			removeCode(){
				if(!this.value){
					return uni.showToast({
						icon:'none',
						title:'内容不能为空'
					})
				}
				uniCloud.importObject('getCode').remove(this.value).then(res=>{
					uni.showModal({
						showCancel: false,
						content: JSON.stringify(res)
					})
				})
			},
			resetCode(){
				if(!this.reset){
					return uni.showToast({
						icon:'none',
						title:'内容不能为空'
					})
				}
				uniCloud.importObject('getCode').resetCode(this.reset).then(res=>{
					uni.showModal({
						showCancel: false,
						content: JSON.stringify(res)
					})
				})
			},
			createCode() {
				uniCloud.importObject('getCode').createCode().then(res => {
					const joinedString = res.string.join("|");
					const result = joinedString.replace(/\|/g, "|\n");
					uni.showModal({
						showCancel: false,
						content: result,
						success: () => {
							uni.setClipboardData({
								data: result
							})
						}
					})
				})
			}
		}
	}
</script>

<style>

</style>